package com.zcf.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alipay.api.internal.util.StringUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.common.result.ResultVo;
import com.zcf.pojo.Dining;
import com.zcf.pojo.Evaluate;
import com.zcf.service.EvaluateService;
import com.zcf.utils.Hutool;
import com.zcf.vo.in.PageVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-12-11
 */
@Controller
@RequestMapping("/evaluate")
@CrossOrigin
@ResponseBody
public class EvaluateController {

	@Autowired
	EvaluateService evaluateService;

	/**
	 * 用户评论
	 */
	@PostMapping("insertEvalua")
	public Body insertEvalua(Evaluate evaluate, MultipartFile[] file) throws Exception {
		evaluate.setEvaluateId(Hutool.getId());
		evaluate.setAddtime(Hutool.parseDateToString());
		return evaluateService.insertEvalua(evaluate, file);
	}

	/**
	 * 删除评论
	 */
	@PostMapping("deleteEvalua")
	public Body deleteEvalua(String evaluateId) {
		Boolean flag = evaluateService.deleteEvalua(evaluateId);
		if (flag) {
			return Body.newInstance(-1, "删除成功");
		}
		return Body.newInstance(-1, "删除失败");
	}

	/**
	 * 查询评论
	 */
	@PostMapping("queryEvalua")
	public Body queryEvalua(String userId, String dinId, PageVo pv) {
		ResultVo rv = evaluateService.queryEvalua(userId, dinId, pv);
		if (rv != null) {
			return Body.newInstance(rv);
		}
		return Body.newInstance("查询失败");
	}

	/**
	 * 回复评论 evaluate_id 商家回复那条评论
	 */
	@PostMapping("addHuiFui")
	public Body addHuiFui(String dinId, String replyConcent, String evaluateId) {
		Dining dining = new Dining();
		dining.setId(dinId);
		dining = dining.selectById();
		if (dining == null) {
			return Body.newInstance("商家不存在");
		}
		Evaluate eva = new Evaluate();
		eva = eva.selectById(evaluateId);
		if (eva == null) {
			return Body.newInstance("评论不存在");
		}
		eva.setReplyTime(Hutool.parseDateToString());
		eva.setReplyConcent(replyConcent);
		EntityWrapper<Evaluate> ew = new EntityWrapper<>();
		ew.eq("evaluate_id", evaluateId);
		Boolean falg = eva.update(ew);
		if (falg) {
			return Body.newInstance("回复成功");
		}
		return Body.BODY_500;
	}
}
